package com.samsung.controller.timer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.samsung.common.annotation.LoginAjaxAuth;
import com.samsung.common.annotation.LoginAuth;
import com.samsung.common.util.Constant;
import com.samsung.dto.common.JErrorResult;
import com.samsung.dto.common.JSuccResult;
import com.samsung.dto.timer.StopwatchDto;
import com.samsung.dto.timer.TimerEventDto;
import com.samsung.model.common.Page;
import com.samsung.model.user.User;
import com.samsung.service.timer.StopwatchService;
import com.samsung.service.timer.TimerService;

/**
 * 	
 *	@ClassName: TimerIndexController
 *	@Description:计时首页
 *	@author pu.huang
 *	@date 2015年5月13日 下午3:15:41
 *
 */
@Controller
@RequestMapping("/timer")
public class TimerIndexController {

	@Resource
	private TimerService timerService;
	@Resource
	private StopwatchService stopwatchService;
	

	/**
	 * @Title: index
	 * @Description: TODO(倒计时首页)
	 * @param @param request
	 * @param @param model
	 * @param @return 设定文件
	 * @return ModelAndView 返回类型
	 * @throws
	 */
	@LoginAuth
	@RequestMapping({"/index", "/"})
	public ModelAndView index(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("timer/index");
		Page page = null;
		String pageNow = request.getParameter("pageNow");
		int totalCount = timerService.getTimerInfoCount();
		List<TimerEventDto> stopWatchInfo = new ArrayList<TimerEventDto>();
		if (pageNow != null) {
			page = new Page(totalCount, Integer.parseInt(pageNow));
			stopWatchInfo = timerService.getStopWatchInfo(page.getStartPos(), page.getPageSize());
		} else {
			page = new Page(totalCount, 1);
			stopWatchInfo = timerService.getStopWatchInfo(page.getStartPos(), page.getPageSize());
		}
		mav.addObject("page", page);
		mav.addObject("dataInfo", stopWatchInfo);
		return mav;
	}
	
	
	/**
	 * 我的计时页面
	 * @param tid
	 * @param mav
	 * @return
	 */
	@LoginAuth
	@RequestMapping("/mytimers")
	public String myTimers(String tid, ModelAndView mav) {
		
		return "timer/mytimers";
	}
	
	/**
	 * 我的计时
	 * @param title
	 * @param createTime
	 * @param request
	 * @return
	 */
	@LoginAjaxAuth
	@ResponseBody
	@RequestMapping("/list")
	public Map<String, Object> list(String title, String createTime, Integer currentPage, HttpServletRequest request) {
		User user = (User)request.getSession().getAttribute(Constant.LOGIN_TOKEN);
		
		int totalCount = timerService.getMyTimerEventsCount(user.getUid(), title, createTime);
		Page page = new Page(totalCount, currentPage);
		
		List<TimerEventDto> stopWatchInfo = timerService.getMyTimerInfo(user.getUid(), title, createTime, page.getStartPos(), page.getPageSize());
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("page", page);
		map.put("resultMap", stopWatchInfo);
		
		return map;
	}
	
	/**
	 * 删除计时活动
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Object deleteTimerData(String tid){
		int cnt = timerService.deleteTimer(tid);
		
		if(cnt > 0) {
			return new JSuccResult("删除成功");
		}
		
		return new JErrorResult("删除失败");
	}
	
	/**
	* @Title: resullt
	* @Description: 计时结果页面
	 */
	@LoginAuth
	@RequestMapping("/result")
	public ModelAndView resullt(String tid, ModelAndView mav) {
		StopwatchDto stopwatchDto = stopwatchService.getActivity(tid);
		mav.addObject("stopwatchDto", stopwatchDto);
		mav.setViewName("timer/result");
		
		return mav;
	}

}